/**
 * 
 */
package homework6;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * @author Nican
 * 
 */
public class SpreadSheet implements ISpreadSheet {

	HashMap<String, IFormula> data = new HashMap<String, IFormula>();

	/**
	 * Edits the content of the cell to keep the formula in the specific
	 * cellname
	 * 
	 * @see homework6.ISpreadSheet#editContents(java.lang.String,
	 * homework6.IFormula)
	 */
	@Override
	public void editContents(String cellname, IFormula expr) {

		data.put(cellname, expr);

	}

	/**
	 * @see homework6.ISpreadSheet#lookupValue(java.lang.String)
	 */
	@Override
	public Integer lookupValue(String forcell) {
		IFormula cell = data.get(forcell);

		return cell.traverse(new ValueOf(this));
	}
	
	/**
	 * Lookup a value from the spreadsheet while keeping the list of used cell references
	 * @param forcell
	 * @param usedCellRefs
	 * @return
	 */
	public Integer lookupValue(String forcell, LinkedList<CellRef> usedCellRefs) {
		IFormula cell = data.get(forcell);

		return cell.traverse(new ValueOf(this, usedCellRefs));
	}

}
